package com.mallotec.android.timenote.ui.adapter;

import android.graphics.Paint;
import android.util.SparseBooleanArray;
import android.view.View;

import com.mallotec.android.timenote.data.bean.Timenote;
import com.mallotec.android.timenote.databinding.ItemGoalBinding;
import com.mallotec.android.timenote.ui.adapter.base.BaseDataBindingAdapter;
import com.mallotec.android.timenote.viewmodel.TimenoteListViewModel;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @author reborn
 * @program TimeNote
 * @description
 * @create 2020-11-02 04:52
 **/
public class GoalAdapter extends BaseDataBindingAdapter<
        Timenote,
        ItemGoalBinding,
        TimenoteListViewModel,
        GoalDataBindingHolder> {

    private SparseBooleanArray mCheckStates = new SparseBooleanArray();

    public GoalAdapter(int itemLayoutId, TimenoteListViewModel viewModel) {
        super(itemLayoutId, viewModel);
    }

    @Override
    protected void bind(ItemGoalBinding binding, Timenote timenote) {
        binding.setTimenote(timenote);
        binding.itemRecordGoal.setText(timenote.getTimenoteContent());
        // in some cases, it will prevent unwanted situations
        binding.checkBox.setOnCheckedChangeListener(null);
        binding.checkBox.setTag(timenote.getId());
        setItemStatus(timenote, binding);
        binding.checkBox.setChecked(mCheckStates.get(timenote.getId(), false));
        binding.checkBox.setOnCheckedChangeListener((view, checked) -> {
            int viewTag = (int) view.getTag();
            binding.getViewModel().dataInited = false;
            if (checked) {
                Calendar nowTime = Calendar.getInstance();
                nowTime.setTimeInMillis(System.currentTimeMillis());
                timenote.setEndDatetime(nowTime);
                mCheckStates.put(viewTag, true);
            } else {
                timenote.setEndDatetime(null);
                mCheckStates.delete(viewTag);
            }
//            setItemStatus(timenote, binding);
            binding.getViewModel().updateTimenote(timenote);
        });
    }

    private void setItemStatus(Timenote timenote, ItemGoalBinding binding) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        // String startTime = format.format(timenote.getStartDatetime().getTime());
        String preEndTime = format.format(timenote.getPreEndDatetime().getTime());
        preEndTime = "计划完成时间：" + preEndTime;
        binding.itemRecordPreEndTime.setText(preEndTime);
        if (timenote.getEndDatetime() == null) {
            binding.itemRecordEndTime.setVisibility(View.GONE);
            setStrikeTextView(binding, false);
            // binding.checkBox.setChecked(false);
            mCheckStates.delete(timenote.getId());
        } else {
            String endTime = format.format(timenote.getEndDatetime().getTime());
            endTime = "完成于：" + endTime;
            binding.itemRecordEndTime.setText(endTime);
            binding.itemRecordEndTime.setVisibility(View.VISIBLE);
            setStrikeTextView(binding, true);
            // binding.checkBox.setChecked(true);
            mCheckStates.put(timenote.getId(), true);
        }
    }

    private void setStrikeTextView(ItemGoalBinding binding, boolean set) {
        if (set) {
            binding.itemRecordGoal.setPaintFlags(binding.itemRecordGoal.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            binding.itemRecordPreEndTime.setPaintFlags(binding.itemRecordPreEndTime.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            binding.itemRecordEndTime.setPaintFlags(binding.itemRecordEndTime.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        } else {
            binding.itemRecordGoal.setPaintFlags(binding.itemRecordGoal.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
            binding.itemRecordPreEndTime.setPaintFlags(binding.itemRecordPreEndTime.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
            binding.itemRecordEndTime.setPaintFlags(binding.itemRecordEndTime.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
        }
    }
}
